﻿using LeanApp.Model;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows.Data;

namespace LeanApp.View
{
    /// <summary>
    /// BalanceDataCtrl.xaml 的交互逻辑
    /// </summary>
    public partial class BalanceDataCtrl : UserControl
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="data">数据</param>
        public BalanceDataCtrl(string title, List<LineBalance> data)
        {
            InitializeComponent();
            _title = title;
            _data = data;
            InitialDataGrid();
            InitialData();
        }

        /// <summary>
        /// 初始化表格
        /// </summary>
        private void InitialDataGrid()
        {
            if (_isInitialed)
            {
                return;
            }
            _isInitialed = true;
            DataGridTextColumn column1 = new DataGridTextColumn { Header = "标号", Binding = new Binding("Id"), Width = DataGridLength.SizeToHeader };
            DataGridTextColumn column2 = new DataGridTextColumn { Header = "产线", Binding = new Binding("LineName"), Width = DataGridLength.SizeToHeader };
            DataGridTextColumn column3 = new DataGridTextColumn { Header = "版本", Binding = new Binding("Version"), Width = DataGridLength.SizeToHeader };
            DataGridTextColumn column4 = new DataGridTextColumn { Header = "工站", Binding = new Binding("WorkStation"), Width = DataGridLength.SizeToHeader };
            DataGridTextColumn column5 = new DataGridTextColumn { Header = "设备", Binding = new Binding("Machine"), Width = DataGridLength.SizeToHeader };
            DataGridTextColumn column6 = new DataGridTextColumn { Header = "CT秒数", Binding = new Binding("CycleTime"), Width = DataGridLength.SizeToHeader };

            grid.Columns.Add(column1);
            grid.Columns.Add(column2);
            grid.Columns.Add(column3);
            grid.Columns.Add(column4);
            grid.Columns.Add(column5);
            grid.Columns.Add(column6);
        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        private void InitialData()
        {
            title.Text = _title;
            if (_data != null && _data.Count > 0)
            {
                _data.ForEach(a =>
                {
                    grid.Items.Add(a);
                });
            }
        }

        /// <summary>
        /// 是否被初始化
        /// </summary>
        private bool _isInitialed = false;

        /// <summary>
        /// 数据实体列表
        /// </summary>
        private List<LineBalance> _data = new List<LineBalance>();
        /// <summary>
        /// 标题
        /// </summary>
        private string _title = "";
    }
}
